#ifndef _ECX_H_
#define _ECX_H_

#define ECX_OK			0
#define ERR_ECX_GENERAL		1
#define ERR_ECX_WRONGLNP	2
#define ERR_ECX_NOTEXIST	3
#define ERR_ECX_WRONGSTRUCT	4
#define ERR_ECX_UICLASS		5

#ifndef _WIN32
#define ECXFUNC_API
#else
#ifdef __cplusplus
#define ECXFUNC_API	extern "C" __declspec(dllexport)
#else
#define ECXFUNC_API	__declspec(dllexport)
#endif
#endif

#ifdef __cplusplus
extern "C" {
#endif

typedef void	*ECXDOM_t;
typedef void	*ECXATTR_t;
typedef int	ecx_err_t;

typedef enum {
	ECX_OPT_NONE = 0,
	ECX_OPT_ONE = 1,
	ECX_OPT_CHILD = 2,
	ECX_OPT_RECUR = 3
} ecx_getopt_t;

typedef enum {
	ECX_FORM_APPLY,
	ECX_FORM_APPLY_COMMIT,
	ECX_FORM_CANCEL
} ecx_formbutton_t;

typedef enum {
	ECX_SANCHOR_LINK,
	ECX_SANCHOR_GO,
	ECX_SANCHOR_ACTION,
	ECX_SANCHOR_ABORT
} ecx_sanchor_mode_t;

void ecx_push_error(const char *fmt, ...);
void ecx_alert_error(const char *fmt, ...);
void ecx_printf(const char *fmt, ...);
void ecx_printf_raw(const char *fmt, ...);
const sr_t *ecx_arg(void);

const char *ecx_username(void);
const char *ecx_sessionid(void);

#define ecx_set_self_val(val)	sr_set_val(ecx_get_self(), val)
#define ecx_set_self_int(ival)	sr_set_int(ecx_get_self(), ival)
#define ecx_set_self_dbl(dval)	sr_set_dbl(ecx_get_self(), dval)
#define ecx_set_self_ll(lval)	sr_set_ll(ecx_get_self(), lval)
#define ecx_set_self_bool(bval)	sr_set_bool(ecx_get_self(), bval)
#define ecx_set_self_str(str)	sr_set_str(ecx_get_self(), str)

#define ecx_get_ctxval_int(row, col)	sr_get_int(ecx_get_ctxval(row, col))
#define ecx_get_ctxval_bool(row, col)	sr_get_bool(ecx_get_ctxval(row, col))
#define ecx_get_ctxval_str(row, col)	sr_get_str(ecx_get_ctxval(row, col))

ECMBOOL ecx_set_content(const char *path);

sr_t *ecx_get_parent(sr_t *sr);

sr_t *ecx_get_ctxval(ecm_uint32 row, ecm_uint32 col);

ecm_uint32 ecx_get_ctxval_rowcount(void);
ECMBOOL ecx_process_entity(const sr_t *sr_base, const sr_t *sr_stage);

sr_t *ecx_get_self(void);
const char *ecx_get_self_stage(void);
const char *ecx_get_stage(ecm_uint32 stageidx);

#define ecx_get_self_int()	sr_get_int(ecx_get_self())
#define ecx_get_self_dbl()	sr_get_dbl(ecx_get_self())
#define ecx_get_self_ll()	sr_get_ll(ecx_get_self())
#define ecx_get_self_bool()	sr_get_bool(ecx_get_self())
#define ecx_get_self_str()	sr_get_str(ecx_get_self())
#define ecx_get_self_srt()	sr_get_type(ecx_get_self())

const char *ecx_DOM_ID(ECXDOM_t DOM);
char *ecx_ESTAGE(const sr_t *sr_stage);
char *ecx_EARG(const sr_t *sr_arg);

const char *ecx_EMBED(const char *embed_id);

ECXDOM_t ecx_FORM_BEGIN(void);
void ecx_FORM_END(void);

void ecx_FORM_BUTTON(const char *embed_id, ecx_formbutton_t type);

ECXDOM_t ecx_TEXT(const char *name, const char *fmt, ...);

void ecx_ACTION_BUTTON(const char *name, const char *embed_id, ECXDOM_t hidden, const char *fmt, ...);

void ecx_NOFORM_BEGIN(void);
void ecx_NOFORM_END(void);

ECXDOM_t ecx_GROUP_BEGIN(const char *name, const char *empty_desc, const char *empty_selection_desc);
void ecx_GROUP_END(void);

ECXDOM_t ecx_FIELD(const char *name, const sr_t *sr_stage, const char *desc, const char *rule, const char *fmt, ...);

ECXDOM_t ecx_TEXTAREA(const char *name, const char *fmt, ...);
ECXDOM_t ecx_PASSWORD(const char *name, const char *DOM_ID, const char *mismatch_desc);
ECXDOM_t ecx_BUTTON(const char *name, const char *caption, ECXATTR_t ATTR);
ECXDOM_t ecx_RADIO(const char *name, ECXATTR_t ATTR, const char *fmt, ...);
ECXDOM_t ecx_CHECKBOX(const char *name, ECXATTR_t ATTR, const char *fmt, ...);
ECXDOM_t ecx_IFRAME(const char *name, int second, ECXATTR_t ATTR);
ECXDOM_t ecx_HIDDEN(const char *name, const char *fmt, ...);
ECXDOM_t ecx_UPLOAD(const char *name, const char *empty_str);
ECXDOM_t ecx_FILE_BROWSE(const char *name, const char *empty_str);
ECMBOOL ecx_DOWNLOAD_BEGIN(ebc_ndid_t ndid, const char *path, const char *fname);
void ecx_DOWNLOAD_END(void);
ECMBOOL ecx_DOWNLOAD(ebc_ndid_t ndid, const char *path);
ECXDOM_t ecx_OBJECT_BEGIN(const char *name, const char *embid, const char *version);
void ecx_OBJECT_END(void);
ECXDOM_t ecx_PARAM(const char *name, const char *value);

void ecx_BR(void);
ECXDOM_t ecx_NOBR_BEGIN(void);
void ecx_NOBR_END(void);
ECXDOM_t ecx_SPAN_BEGIN(void);
void ecx_SPAN_END(void);

void ecx_SANCHOR(ecx_sanchor_mode_t mode, const sr_t *sr_anchor, const sr_t *sr_stage);

ECXDOM_t ecx_SANCHOR_BEGIN(ecx_sanchor_mode_t mode, const char *name, const sr_t *sr_stage, const char *fmt, ...);
void ecx_SANCHOR_END(void);
ECMBOOL ecx_BACK(ECMBOOL abortive, ecm_uint32 nsteps);

ECXDOM_t ecx_SELECT_BEGIN(const char *name, ECMBOOL submit_all, ECMBOOL multiple);
void ecx_SELECT_END(void);
ECXDOM_t ecx_OPTION(ECMBOOL selected, const char *fmt, ...);

void ecx_POPUP_BEGIN(sr_t *sr_stage, const char *name, int wt, int ht);
void ecx_POPUP_END(void);
void ecx_POPUP_FORM_BUTTON(const char *embed_id, ecx_formbutton_t type, char *action);

const sr_t *ecx_get_GROUP(const char *name);
const char *ecx_get_FIELD(const char *fmt, ...);
const char *ecx_get_PASSWORD(const char *fmt, ...);
const char *ecx_get_RADIO(const char *fmt, ...);
const char *ecx_get_CHECKBOX_1(const char *fmt, ...);
const sr_t *ecx_get_CHECKBOX(const char *fmt, ...);
const sr_t *ecx_get_SELECT(const char *name);
const char *ecx_get_HIDDEN(const char *fmt, ...);
const char *ecx_get_UPLOAD(const char *name);
const char *ecx_get_FILE_BROWSE(const char *fmt, ...);
const char *ecx_get_user(void);

ECXDOM_t ecx_IMG(const char *name, const char *embed_id, ECXATTR_t ATTR);
ECXDOM_t ecx_DIMG(const char *name, const sr_t *sr_stage);
void ecx_IMGSRC(const ecm_uint8 *buf, ecm_size_t size);

ECXDOM_t ecx_TABLE_BEGIN(ECXATTR_t attr);
void ecx_TABLE_END(void);
ECXDOM_t ecx_TR(ECXATTR_t attr);
ECXDOM_t ecx_TD(ECXATTR_t attr);

ECXDOM_t ecx_DIV_BEGIN(void);
void ecx_DIV_END(void);

void ecx_DYNAMIC_BEGIN(ECMBOOL enabled);
void ecx_DYNAMIC_END(void);

ECMBOOL ecx_is_editing(void);
ECMBOOL ecx_is_applying(void);
ECMBOOL ecx_is_showing(void);

void ecx_reg_private(ecm_cptr_t priv);
ecm_cptr_t ecx_get_private(void);

const char *ecx_parent_ctxcls(void);
ECMBOOL ecx_has_supplementary(const char *clsname);

ECXATTR_t ecx_attr(ECXATTR_t attr, ...);

void ecx_add_attr(ECXDOM_t DOM, const char *attr, const char *fmt, ...);
void ecx_set_attr(ECXDOM_t DOM, ECXATTR_t attr);

typedef ECMBOOL (*ecx_ctxcls_begin_t)(void);
typedef ECMBOOL (*ecx_ctxcls_proc_t)(void);
typedef ECMBOOL (*ecx_ctxcls_end_t)(void);

#ifdef __cplusplus
}
#endif

#endif
